import filters from '@/common/js/filters.js'

module.exports = {
	init:function(Vue,$common,XDate){
		Object.keys(filters).forEach(key => {
			Vue.filter(key, filters[key])
		})
		
		//格式手机号码
		Vue.filter("_filterMobile", function (value) {
		    if ($common.isEmpty(value)) {
			    return value;
		    } else {
		        return value.substr(0, 3) + " " + value.substr(3, 4) + " " + value.substr(7, 4);
		    }
		});
		
		//格式验证码
		Vue.filter("_filterCode", function (value) {
		    if ($common.isEmpty(value)) {
			   return value;
		    } else {
		        return value.substr(0, 4) + " " + value.substr(4, 4) + " " + value.substr(8, 4);
		    }
		});
		
		//格式数量
		Vue.filter("_filterCount", function (value) {
		    if ($common.isEmpty(value)) {
			   return value;
		    } else {
		        return value < 10000 ? value : (parseInt(value/10000) + '.' + (value%10000).toString().substr(0,1) + '万');
		    }
		});
		
		
		//格式化身份证
		Vue.filter("_filterIDNumber", function (vl) {
		    var len = vl.length;
		    var asteriskLen = parseInt(len / 3);
		    var result = vl.substr(0, asteriskLen);
		    for (var i = 0; i < asteriskLen; i++) {
		        result = result + "*";
		    }
		    result = result + vl.substr(len - asteriskLen, asteriskLen);
		    return result;
		});
		
		//格式时间
		Vue.filter("_filterDate", function (vl,fmt) {
			if(vl=="" || vl==undefined || vl==null){
				return "";
			}else{
				return new XDate(vl).toString(fmt);
			}
		});
		
		//格式星期
		Vue.filter("_filterWeek", function (vl) {
			if(vl=="" || vl==undefined || vl==null){
				return "";
			}else{
				var curDate = new XDate(new XDate().toString("yyyy-MM-dd"));
				var date = new XDate(vl);
				var days = curDate.diffDays(date);
				switch(days)
				{
					case 0:
						return "今天";
						break;
					case 1:
						return "明天";
						break;
					case 2:
						return "后天";
						break;
					default:
						return "周" + "日一二三四五六".charAt(date.getDay());
						break;
				}
			}
		});
	}
}